<template>
    <el-tree
        :data="data"
        show-checkbox
        node-key="id"
        :default-checked-keys="defaultValue"
        :props="defaultProps"
        @check="checkTree"
        >
    </el-tree>
</template>

<script>
  export default {
    props: ["menuValue"],
    data() {
      return {
        data: [],
        defaultProps: {
          children: 'childMenus',
          label: 'menuName'
        },
        defaultValue: [],
      };
    },
    watch: {
      watchValue: {
        handler(value) {
          if(value){
            this.defaultValue = JSON.parse(value);
          }
        }
      }
    },
    computed: {
      watchValue() {
        return this.menuValue
      }
    },
    mounted() {
      this.getMenuTree();
    },
    methods: {
      checkTree(node,nodes){
        let uuids = nodes.checkedKeys;
        uuids = JSON.stringify(uuids);
        this.$emit('update:menuValue',uuids);
      },
      // 获取树状菜单
      getMenuTree(){
        this.$api.menu.getMenuTreeAll().then(res=>{
          if(res.code == 200){
            this.data = res.data;
          }
        })
      }
    },
  };
</script>

<style>

</style>